/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.javacommunity.controller.bean;

import com.javacommunity.model.dao.QuestionDAO;
import com.javacommunity.model.dao.TagDAO;
import com.javacommunity.model.dao.UserDAO;
import com.javacommunity.model.dao.impl.QuestionDAOImpl;
import com.javacommunity.model.dao.impl.TagDAOImpl;
import com.javacommunity.model.dao.impl.UserDAOImpl;
import com.javacommunity.model.entity.Question;
import com.javacommunity.model.entity.Tag;
import com.javacommunity.model.entity.User;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.event.ActionEvent;

/**
 *
 * @author Cong Pham Dinh
 */
@ManagedBean
@RequestScoped
public class QuestionManagement {

    private final QuestionDAO questionDAO = QuestionDAOImpl.getInstance();
    private final TagDAO tagDAO = TagDAOImpl.getInstance();
    private final UserDAO userDAO = UserDAOImpl.getInstance();

    private List<Question> questions = new ArrayList<Question>();
    private List<Tag> tags = new ArrayList<Tag>();
    private List<Tag> selectedTags = new ArrayList<Tag>();

    private Question question = new Question();
    private Tag tag = new Tag();

    public QuestionManagement() {
        tags = tagDAO.getAllTags(2);
        questions = questionDAO.getQuestions(2);
    }

    public List<Tag> completeTag(String query) {
        List<Tag> filteredList = new ArrayList<Tag>();
        for (Tag t : tagDAO.getAllTags(2)) {
            if (t.getTagName().startsWith(query) && !selectedTags.contains(t)) {
                filteredList.add(t);
            }
        }
        return filteredList;
    }

    public String createQuestion(ActionEvent evt) {
        String page;
        User u = userDAO.getUserById(1);
        String qName = this.question.getQuestionName();
        String qDetail = this.question.getQuestionDetail();
        Date createDate = new Date();

        Question newQuestion = new Question(u, qName, qDetail, createDate, createDate, 0, true, null, null, selectedTags);
        if (!questionDAO.checkExistQuestion(qName)) {
            if (questionDAO.createQuestion(newQuestion)) {
                page = "/page/index.jsf";
            }else{
                page = "/page/ask-question";
            }
        }else{
            page = "/page/ask-question";
        }
        return page;
    }

    public void addViewer(int questionId) {

    }

    public List<Question> getQuestions() {
        return questions;
    }

    public void setQuestions(List<Question> questions) {
        this.questions = questions;
    }

    public Question getQuestion() {
        return question;
    }

    public void setQuestion(Question question) {
        this.question = question;
    }

    public List<Tag> getTags() {
        return tags;
    }

    public void setTags(List<Tag> tags) {
        this.tags = tags;
    }

    public Tag getTag() {
        return tag;
    }

    public void setTag(Tag tag) {
        this.tag = tag;
    }

    public List<Tag> getSelectedTags() {
        return selectedTags;
    }

    public void setSelectedTags(List<Tag> selectedTags) {
        this.selectedTags = selectedTags;
    }

}
